#include <algorithm>
using std::max;

class Solution {
private:
    int findK(int A[], int m, int B[], int n, int k){
        if (k<=n && (m==0 || B[k-1]<=A[0])) return B[k-1];
        if (k<=m && (n==0 || A[k-1]<=B[0])) return A[k-1];
        int h=0, t=m-1;
        while (h<=t){
            int w=(h+t)/2, e=k-w-2;
            if (e<0) t=w-1; else 
            if (e>=n) h=w+1; else {
                int mx=max(A[w], B[e]);
                if ((w==m-1 || mx<=A[w+1]) && (e==n-1 || mx<=B[e+1])) return mx;
                if ((w!=m-1 && mx>A[w+1])) h=w+1; else t=w-1;
            }
        }
    }
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        if ((m+n)%2==0) return ((double)findK(A, m, B, n, (m+n)/2)+(double)findK(A, m, B, n, (m+n)/2+1))/2;
            else return (double)findK(A, m, B, n, (m+n+1)/2);
    }
};
